package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.scene.service.OperateSceneInstService;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.order.base.consts.KeyValues;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 新增APN方式属性处理器
 */
public class ApnModeAddedAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {
    @Autowired
    private OperateInstAttrService operateInstAttrService;
    @Autowired
    private IQuerySceneInstService querySceneInstService;
    @Autowired
    private OperateSceneInstService operateSceneInstService;
    @Override
    public void onShow(KiteAttr attr, KiteInst inst, AttrData data) {
        linkClearValue(attr,inst,data);
    }
   /* @Override
    public void after(KiteAttr attr, KiteInst inst, AttrData data) {
        super.after(attr,inst,data);
        linkClearValue(attr,inst,data);
    }*/
    private void linkClearValue(KiteAttr attr, KiteInst inst, AttrData data) {
        // 隐藏“速率”和“使用PCRF”
        String newAddType = inst.getAttrValue("200003996");

        KiteAttr pcrfAppendAttr = inst.getAttr("200004167");
        KiteAttr pcrfAttr = inst.getAttr("200004005");
        if (!KiteStringUtils.equals(newAddType, "100006952")) {
            KiteAttr speedAttr = inst.getAttr("200004004");
            if (!KiteObjectUtils.isEmpty(speedAttr)) {
                speedAttr.setShowType("E");
                operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), speedAttr);
            }
            if (!KiteObjectUtils.isEmpty(pcrfAttr)) {
                pcrfAttr.setShowType("E");
            }
            if (!KiteObjectUtils.isEmpty(pcrfAppendAttr)) {
                pcrfAppendAttr.setShowType("E");
            }
        } else {
            // 是否pcrf默认为是
            if (!KiteObjectUtils.isEmpty(pcrfAppendAttr) && !KiteObjectUtils.isEmpty(pcrfAttr) && "1_0000106".equals(pcrfAttr.getValue())) {
                pcrfAppendAttr.setShowType("D");
            }
        }
    }
    @Override
    public void set(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        super.set(attr, inst, value, data);
        Map<String, Object> extParameter = querySceneInstService.getExtParameter(data.getSceneInstId());
        if(KiteMapUtils.isNotEmpty(extParameter)){
            String id = KiteMapUtils.getString(extParameter,"sceneTempl");
            if(!KiteStringUtils.isEmpty(id)){
                return;
            }
        }
        List<KiteAttr> attrs = inst.listAttrs();
        if (KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.ADD_ORDE))) {
            return;
        }
        //重提初始化不校验
        //变更业务的场景初始化过程中仅做属性编辑控制,通过loadFlag排除after方法设置默认值而进来的干扰,为true表示已经after方法已经执行完毕
        if (!KiteStringUtils.equalsAny(data.getServiceOfferId(), ServiceOfferConsts.SERVICE_OFFER_ORDER, LocalServiceOfferConsts.SERVICE_OFFER_RESOURCE_CHECK)
            && !KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE))
            && !"workOrder".equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), "initSource"))) {
            if (KiteStringUtils.equalsAny(value,"100006950", "100006951") && attr.getLoadFlag()) {
                attrs.stream().filter(kiteAttr -> KeyConsts.IFTRUE_T.equals(kiteAttr.getIsEdit())).forEach(kiteAttr -> {
                    kiteAttr.setIsEdit(KeyConsts.IFTRUE_F);
                });
            }
            // 变更业务 新增APN方式为"我要定制"时,新增APN方式不可编辑
            if (KiteStringUtils.equals("100006952", value) && attr.getLoadFlag()) {
                attr.setIsEdit(KeyConsts.IFTRUE_F);
            }
            return;
        }
        //fixme 可能导致频繁访问缓存,上线临时处理方案
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if (KiteMapUtils.isNotEmpty(extParams) && extParams.containsKey("nodeCode")) {
            if (!ServiceOfferConsts.SERVICE_OFFER_ORDER.equals(data.getServiceOfferId())) {
                if (KiteStringUtils.equalsAny(value,"100006950", "100006951") && attr.getLoadFlag()) {
                    attrs.stream().filter(kiteAttr -> KeyConsts.IFTRUE_T.equals(kiteAttr.getIsEdit())).forEach(kiteAttr -> {
                        kiteAttr.setIsEdit(KeyConsts.IFTRUE_F);
                    });
                }
            }
            return;
        }
        //新增apn方式切换时设置属性默认值，没有默认值的不设置
        attrs.forEach(kiteAttr -> {
            if(!KiteStringUtils.isEqual(kiteAttr.getFieldName(),attr.getFieldName()) && !KiteStringUtils.isEqual(KeyConsts.PROD_ATTR_SHOW_TYPE_E,kiteAttr.getShowType())){
                List<KiteAttrValue> attrValues =  kiteAttr.getAttrValues();
                String attrType = kiteAttr.getAttrValueTypeId();
                if(KiteListUtils.isNotEmpty(attrValues) && !KiteStringUtils.equals(attrType, KeyValues.INPUT)){
                    String defaultValue = kiteAttr.getDefaultValue();
                    List<KiteAttrValue> filterAttrValues = attrValues.stream().filter(AttrValue -> KiteStringUtils.isEqual(AttrValue.getAttrValue(),defaultValue)).collect(Collectors.toList());
                    KiteAttrValue attrValue = attrValues.get(0);
                    if(KiteStringUtils.isEmpty(defaultValue) || KiteListUtils.isEmpty(filterAttrValues)){
                        operateInstAttrService.setAttrValue(inst, kiteAttr.getFieldName(), attrValue.getAttrValue(), data.getSceneInstId(), null);
                    }
                    else{
                        operateInstAttrService.setAttrValue(inst, kiteAttr.getFieldName(), kiteAttr.getDefaultValue(), data.getSceneInstId(), null);
                    }
                }
                else{
                    // 属性值不为空才设置默认值
                    if (KiteStringUtils.isEmpty(kiteAttr.getValue()) && !KiteStringUtils.isEmpty(kiteAttr.getDefaultValue())) {
                        operateInstAttrService.setAttrValue(inst, kiteAttr.getFieldName(), kiteAttr.getDefaultValue(), data.getSceneInstId(), null);
                    }
                }
            }
        });
    }

}
